/**
 *
 */
package com.amir.common.weight;

import android.content.Context;
import android.graphics.Color;
import android.text.Layout;
import android.text.Spannable;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.method.Touch;
import android.text.style.BackgroundColorSpan;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * @author justKeepmoving
 * @date 2015年4月16日下午4:02:14
 */
public class TextViewFixTouchConsume extends AppCompatTextView {

    boolean linkHit;

    public TextViewFixTouchConsume(Context context) {
        this(context, null);
    }

    public TextViewFixTouchConsume(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextViewFixTouchConsume(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setMovementMethod(LocalLinkMovementMethod.getInstance());
        setFocusable(false);
        setClickable(false);
        setLongClickable(false);
    }

    public static class LocalLinkMovementMethod extends LinkMovementMethod {
        static LocalLinkMovementMethod sInstance;

        public static LocalLinkMovementMethod getInstance() {
            if (sInstance == null)
                sInstance = new LocalLinkMovementMethod();
            return sInstance;
        }

        @Override
        public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {

            int action = event.getAction();
            if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
                int x = (int) event.getX();
                int y = (int) event.getY();
                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();
                x += widget.getScrollX();
                y += widget.getScrollY();
                Layout layout = widget.getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);// 0只能用于第一行是可点击的状态
                ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);
                if (link.length != 0) {
                    if (action == MotionEvent.ACTION_UP) {
                        link[0].onClick(widget);
                        buffer.setSpan(new BackgroundColorSpan(Color.TRANSPARENT), 0, buffer.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

                    } else {
                        buffer.setSpan(new BackgroundColorSpan(Color.LTGRAY), buffer.getSpanStart(link[0]), buffer.getSpanEnd(link[0]), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                    if (widget instanceof TextViewFixTouchConsume) {
                        ((TextViewFixTouchConsume) widget).linkHit = true;
                    }
                    return true;
                } else {
                    buffer.setSpan(new BackgroundColorSpan(Color.TRANSPARENT), 0, buffer.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    Touch.onTouchEvent(widget, buffer, event);
                    return false;
                }
            } else if (action == MotionEvent.ACTION_CANCEL) {
                buffer.setSpan(new BackgroundColorSpan(Color.TRANSPARENT), 0, buffer.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return super.onTouchEvent(widget, buffer, event);
        }
    }
}